package com.jeeplus.modules.api.utils;

import com.jeeplus.modules.purchase.entity.Purchase;
import com.jeeplus.modules.source.entity.Source;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class MessageUtils {


    private WxMpService wxMpService;

    @Autowired
    public void setWxMpService(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
    }

    private static MessageUtils messageUtils;


    @PostConstruct
    public void init() {
        messageUtils = this;
        messageUtils.wxMpService = this.wxMpService;
    }

    public WxMpTemplateMessage wxTemplate(String openid, String notifyTempTemplateId, String url) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(notifyTempTemplateId);
        templateMessage.setToUser(openid);
        templateMessage.setUrl(url);
        return templateMessage;
    }

    /**
     * 新车源消息
     * @param openid 消息接收人
     * @param purchase 消息内容
     */
    public void newSourceNotify(String openid, String notifyTemplateId, String URL, Purchase purchase) {
        WxMpTemplateMessage templateMessage = wxTemplate(openid, notifyTemplateId, URL);
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "为您匹配到一个求购信息!", "#E1B741"),
                new WxMpTemplateData("keyword1", purchase.getEndTime(), "#E1B741"),
                new WxMpTemplateData("keyword2", purchase.getBrand(), "#E1B741"),
                new WxMpTemplateData("keyword3", purchase.getCarStyle(), "#E1B741"),
                new WxMpTemplateData("keyword4", purchase.getColour(), "#E1B741"),
                new WxMpTemplateData("keyword5", purchase.getInteriorColor(), "#E1B741"),
                new WxMpTemplateData("remark", purchase.getRemarks(), "#E22018")
        );
        templateMessage.setData(data);
        try {
            messageUtils.wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("【派单消息】发送成功");
        } catch (WxErrorException e) {
            log.error("【派单消息】发送失败，{}", e);
        }
    }

    /**
     * 求购消息
     * @param openid 消息接收人
     * @param source 车源信息
     */
    public void newPurchaseNotify(String openid, String notifyTemplateId, String URL, Source source) {
        WxMpTemplateMessage templateMessage = wxTemplate(openid, notifyTemplateId, URL);
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "为您匹配到一个车源信息!", "#E1B741"),
                new WxMpTemplateData("keyword1", source.getCarStyle(), "#E1B741"),
                new WxMpTemplateData("keyword2", source.getLsPrice(), "#E1B741"),
                new WxMpTemplateData("keyword3", source.getRegistrationDate(), "#E1B741"),
                new WxMpTemplateData("keyword4", source.getMileage(), "#E1B741"),
                new WxMpTemplateData("keyword5", date2Str(source.getUpdateDate()), "#E1B741"),
                new WxMpTemplateData("remark", source.getRemarks(), "#E22018")
        );
        templateMessage.setData(data);
        try {
            messageUtils.wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            log.info("【派单消息】发送成功");
        } catch (WxErrorException e) {
            log.error("【派单消息】发送失败，{}", e);
        }
    }

    private String date2Str(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }
}